import 'dart:async';

import 'package:homepad/logger/logger.dart';
import 'package:web_socket_channel/status.dart' as status;
import 'package:web_socket_channel/web_socket_channel.dart';

void main() async {
  logger.info("Ok,let's go!");

  final wsUrl = Uri.parse("ws://127.0.0.1:8080/ws?client-id=flutter-client");
  final channel = WebSocketChannel.connect(wsUrl);

  // await Future.delayed(const Duration(seconds: 5));
  await channel.ready;

  runZoned(() async {
    await for (final message in channel.stream) {
      logger.info("message(${message.runtimeType}): $message");
    }
    logger.info("the end!");
    logger.info("closeCode: ${channel.closeCode} closeReason: ${channel.closeReason}");
  });

  Future.delayed(const Duration(seconds: 5), () {
    channel.sink.add("""{"method":"read.attribute","data":{"endpoint":65535,"cluster":4294967295,"attribute":65535}}""");
  });


/*  channel.stream.listen((message) {
    logger.info("message: $message");
    channel.sink.add("ok.let's go!");
  });*/


  Future.delayed(const Duration(seconds: 60), () {
    logger.info("status: ${status.normalClosure}");
    channel.sink.close(status.normalClosure);
  });
}